/*
 * RegimenEliminarFrame.java
 *
 * Created on 30 de abril de 2008, 03:21 PM
 */
package org.contabilidad.regimen;

import org.mybeans.Sesion;
import org.mybeans.RegimenBeans;
 
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author  avbravo
 */
public class RegimenEliminarFrame extends javax.swing.JFrame {

    Sesion sesion;
    RegimenBeans regimenBeans;
 boolean focoRegimen = false;
    /** Creates new form RegimenEliminarFrame */
    public RegimenEliminarFrame() {
        initComponents();
           /*
             * Fondo
             */

            
              
             

             
             

            /*
             * fin del fondo
             */
        sesion = new Sesion();
        regimenBeans = new RegimenBeans();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jTextFieldIdRegimen = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTextFieldRegimen = new javax.swing.JTextField();
        jButtonConsultarRegimen = new javax.swing.JButton();
        jButtonBuscarRegimen = new javax.swing.JButton();
        jButtonEliminar = new javax.swing.JButton();
        jButtonRegresar = new javax.swing.JButton();
        jButtonLimpiar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Eliminar Regimen");
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jTextFieldIdRegimen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldIdRegimenActionPerformed(evt);
            }
        });

        jLabel5.setText("Codigo");

        jLabel6.setText("Regimen");

        jTextFieldRegimen.setEditable(false);

        jButtonConsultarRegimen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/consultar.png"))); // NOI18N
        jButtonConsultarRegimen.setToolTipText("Consultar");
        jButtonConsultarRegimen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConsultarRegimenActionPerformed(evt);
            }
        });

        jButtonBuscarRegimen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/search.png"))); // NOI18N
        jButtonBuscarRegimen.setToolTipText("Buscar");
        jButtonBuscarRegimen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarRegimenActionPerformed(evt);
            }
        });

        jButtonEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/delete.png"))); // NOI18N
        jButtonEliminar.setToolTipText("Eliminar");
        jButtonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEliminarActionPerformed(evt);
            }
        });

        jButtonRegresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar.setToolTipText("Regresr");
        jButtonRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresarActionPerformed(evt);
            }
        });

        jButtonLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/escoba.png"))); // NOI18N
        jButtonLimpiar.setToolTipText("Limpiar");
        jButtonLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLimpiarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5))
                        .addGap(28, 28, 28)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jTextFieldIdRegimen, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(29, 29, 29)
                                .addComponent(jButtonConsultarRegimen)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonBuscarRegimen))
                            .addComponent(jTextFieldRegimen, javax.swing.GroupLayout.PREFERRED_SIZE, 481, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(75, 75, 75)
                        .addComponent(jButtonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(64, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonBuscarRegimen, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextFieldIdRegimen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addComponent(jButtonConsultarRegimen)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextFieldRegimen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonConsultarRegimenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConsultarRegimenActionPerformed
Consultar();
    
}//GEN-LAST:event_jButtonConsultarRegimenActionPerformed

private void jButtonBuscarRegimenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarRegimenActionPerformed
 

    RegimenBusquedaFrame regimenBusquedaFrame = new RegimenBusquedaFrame();
    regimenBusquedaFrame.setLocationRelativeTo(null);
    regimenBusquedaFrame.setVisible(true);
     focoRegimen = true;
}//GEN-LAST:event_jButtonBuscarRegimenActionPerformed

private void jButtonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEliminarActionPerformed
 
    try {
        String idRegimen = this.jTextFieldIdRegimen.getText();
        if (idRegimen == null || idRegimen.equals("")) {
            JOptionPane.showMessageDialog(this, "Ingrese el codigo ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldIdRegimen.requestFocus();
            return;
        }

        String regimen = this.jTextFieldRegimen.getText();
        if (regimen == null || regimen.equals("")) {
            JOptionPane.showMessageDialog(this, "Ingrese el regimen", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldIdRegimen.requestFocus();
            return;
        }

        if (sesion.Conectar() == false) {
            JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        String sqlquery = "select * from regimen where idmunicipio=? and idregimen= ? ";
        PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
        psquery.setString(1, sesion.getIdmunicipio());
        psquery.setString(2, idRegimen);
        ResultSet rs = psquery.executeQuery();
        rs.next();
        int x = rs.getRow();

        if (x == 0) {
            JOptionPane.showMessageDialog(this, "No existe un regimen con ese codigo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);

            return;
        }
        sqlquery = "select count(*) as total from regimencontribuyentes where idmunicipio=? and idregimen= ? ";
        psquery = sesion.con.prepareStatement(sqlquery);
        psquery.setString(1, sesion.getIdmunicipio());
        psquery.setString(2, idRegimen);
        rs = psquery.executeQuery();
        rs.next();
        int total = rs.getInt("total");

        if (total != 0) {
            JOptionPane.showMessageDialog(this, "Hay regimen de contribuyente asignados.", "No se puede eliminar", JOptionPane.INFORMATION_MESSAGE);
          return;
        }
        //
        sqlquery = "select count(*) as total from regimencomercios where idmunicipio=? and idregimen= ? ";
        psquery = sesion.con.prepareStatement(sqlquery);
        psquery.setString(1, sesion.getIdmunicipio());
        psquery.setString(2, idRegimen);
        rs = psquery.executeQuery();
        rs.next();
        total = rs.getInt("total");

        if (total != 0) {
            JOptionPane.showMessageDialog(this, "Hay regimen de comercios asignados.", "No se puede eliminar", JOptionPane.INFORMATION_MESSAGE);
          return;
        }

//vehiculos
        sqlquery = "select count(*) as total from regimenvehicles where idmunicipio=? and idregimen= ? ";
        psquery = sesion.con.prepareStatement(sqlquery);
        psquery.setString(1, sesion.getIdmunicipio());
        psquery.setString(2, idRegimen);
        rs = psquery.executeQuery();
        rs.next();
        total = rs.getInt("total");

        if (total != 0) {
            JOptionPane.showMessageDialog(this, "Hay regimen de vehiculos asignados.", "No se puede eliminar", JOptionPane.INFORMATION_MESSAGE);
           return;
        }

        //recibosdetalles
        sqlquery = "select count(*) as total from recibesdetalles where idmunicipio=? and idregimen= ? ";
        psquery = sesion.con.prepareStatement(sqlquery);
        psquery.setString(1, sesion.getIdmunicipio());
        psquery.setString(2, idRegimen);
        rs = psquery.executeQuery();
        rs.next();
        total = rs.getInt("total");

        if (total != 0) {
            JOptionPane.showMessageDialog(this, "Hay recibos de detalles  asignados.", "No se puede eliminar", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        //
        int res = JOptionPane.showConfirmDialog(this, "Desea eliminar este regimen",
                "Eliminar Regimen ", JOptionPane.YES_NO_OPTION);
        if (res == JOptionPane.YES_OPTION) {

            String sql = "delete from regimen where idmunicipio=? and idregimen= ? ";
            PreparedStatement prest = sesion.con.prepareStatement(sql);
            prest.setString(1, sesion.getIdmunicipio());
            prest.setString(2, idRegimen);

            prest.executeUpdate();
            sesion.con.close();
            JOptionPane.showMessageDialog(this, "Regimen Eliminado exitosamente", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            Limpiar();
        } else {
            JOptionPane.showMessageDialog(this, "No se elimino el regimen.", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        }
    } catch (Exception ex) {
        System.out.println("Error" + ex);
        JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButtonEliminarActionPerformed

private void jButtonRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresarActionPerformed
 
    dispose();
}//GEN-LAST:event_jButtonRegresarActionPerformed

private void jButtonLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLimpiarActionPerformed
 
    Limpiar();
}//GEN-LAST:event_jButtonLimpiarActionPerformed

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
    // TODO add your handling code here:
    if ( focoRegimen){
         this.jTextFieldIdRegimen.setText(regimenBeans.getIdRegimen());
    this.jTextFieldRegimen.setText(regimenBeans.getRegimen());
    }
     focoRegimen = false;
}//GEN-LAST:event_formWindowGainedFocus

private void jTextFieldIdRegimenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldIdRegimenActionPerformed
    // TODO add your handling code here:
    Consultar();
}//GEN-LAST:event_jTextFieldIdRegimenActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new RegimenEliminarFrame().setVisible(true);
            }
        });
    }

    private void Limpiar() {
        jTextFieldIdRegimen.setText("");
        jTextFieldRegimen.setText("");
    }

    private void Consultar(){
        try {
        String IdRegimen = this.jTextFieldIdRegimen.getText();
        if (IdRegimen == null || IdRegimen.equals("")) {
            JOptionPane.showMessageDialog(this, "Ingrese el codigo del regimen", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        if (sesion.Conectar() == false) {
            JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        String sqlquery = "select regimen.* from regimen where regimen.idmunicipio = ? and idregimen = ?";
        PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
        psquery.setString(1, sesion.getIdmunicipio());
        psquery.setString(2, IdRegimen);
        ResultSet rs = psquery.executeQuery();
        rs.next();
        int x = rs.getRow();
        if (x == 0) {
            JOptionPane.showMessageDialog(this, "No existen registros de regimen con ese codigo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            Limpiar();
            return;
        }
        String activo = rs.getString("activo");
        if (activo.equals("no")) {
            JOptionPane.showMessageDialog(this, "Este regimen esta inactivo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        String IdSubGrupoRegimen = rs.getString("idsubgruporegimen");
        String Regimen = rs.getString("regimen");
        double Valor = rs.getDouble("valor");
        double LimiteInferior = rs.getDouble("limiteinferior");
        double LimiteSuperior = rs.getDouble("limiteSuperior");

        if (Regimen == null || Regimen.equals("")) {
            JOptionPane.showMessageDialog(this, "El regimen esta en blanco", "Mensaje", JOptionPane.WARNING_MESSAGE);
            return;
        }

        this.jTextFieldRegimen.setText(Regimen);

        repaint();

    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBuscarRegimen;
    private javax.swing.JButton jButtonConsultarRegimen;
    private javax.swing.JButton jButtonEliminar;
    private javax.swing.JButton jButtonLimpiar;
    private javax.swing.JButton jButtonRegresar;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextFieldIdRegimen;
    private javax.swing.JTextField jTextFieldRegimen;
    // End of variables declaration//GEN-END:variables
}
